Option Strict On

Imports System
Imports System.Collections
Imports System.Text
Imports PocketSOAP

Namespace WiCourts.Wcca2.Examples
    Public Class WCCA2Example2
        ' NOTES
        ' - CoEnvelope.EncodingStyle is set "" to support document/literal requests
        ' - Nested nodes use the WCCA2 namespace
        ' - When creating nodes it's important to use a type that maps to the type
        '   specified in the WSDL.  This is because PocketSOAP includes type information
        '   for document/literal services, which the server uses.  If it doesn't match
        '   what's expected however, you'll get a java.lang.IllegalArgumentException
        ' - Each example demonstrates a slightly different use case:
        '  - RunCaseSearch3 - A simple party name search
        '  - RunCaseSearch1 - Another case search using more complex types
        '  - RunAttyCalendarSearch - An example using date parameters
        '  - RetrieveCriminalCase - Shows how to navigate the DOM-like structure
        '    returned by the SOAP call

        ' Set this to the username provided by CCAP
        Private Const USER As String = "wihistory"

        ' Set this to the password provided by CCAP
        Private Const PASSWORD As String = "WHSpw4SOAP"

        ' The URL of the WCCA2 SOAP service
        Private Const URL As String = "http://wcca.wicourts.gov/services/WCCA2"

        ' The namespace used by the service.  This shouldn't be changed.
        Private Const WCCA2_NAMESPACE As String = "urn:soap.wcca2.ccap.courts.state.wi.us"

        Public Shared Sub Main()
            Dim http As HTTPTransport

            http = GetTransport()

            ' Run example searches
            RetrieveCriminalCase(http)
            RunAttyCalendarSearch(http)
            RunCaseSearch1(http)
            RunCaseSearch3(http)
        End Sub

        Private Shared Sub RetrieveCriminalCase(ByVal http As HTTPTransport)
            Dim env As New CoEnvelope()
            Dim input, result As CoSoapNode

            Console.WriteLine("Retrieving case 02CF57 from county 44 (Outagamie)")

            input = InitializeEnvelope(env, "retrieveCriminalCase")
            input.Nodes.Create("caseNo", "2002CF000057")
            input.Nodes.Create("countyNo", "44")

            http.Send(URL, env.Serialize)

            env.Parse(http)

            result = CType(env.Parameters.Item(0).Value, CoSoapNode)

            Console.WriteLine("--------")

            Console.WriteLine("Defendant name is " + FormatName(result.Nodes.ItemByName("defendant")))

            ' This is a bit awkward, but it doesn't seem there is a better way
            Dim i As IEnumerator
            Dim c As CoSoapNode

            Console.WriteLine("Charges:")
            i = result.Nodes.GetEnumerator
            While i.MoveNext
                c = CType(i.Current, CoSoapNode)
                If c.Name = "chargeList" Then
                    Console.WriteLine("    {0}", c.Nodes.ItemByName("descr").Value)
                End If
            End While

            Console.WriteLine("--------")
        End Sub

        Private Shared Sub RunAttyCalendarSearch(ByVal http As HTTPTransport)
            Dim env As New CoEnvelope()
            Dim input, result As CoSoapNode

            Console.WriteLine("Running attorney calendar search")

            input = InitializeEnvelope(env, "searchAttyCalendars")
            input.Nodes.Create("attyNo", "1015378")
            input.Nodes.Create("attyType", "prosAtty")
            input.Nodes.Create("date", New Date(2003, 3, 24))
            ' Unlike most data elements this is an integer and not a string
            input.Nodes.Create("range", 7)

            http.Send(URL, env.Serialize)

            env.Parse(http)

            DumpAttyCalendarList(env.Parameters.Item(0))

        End Sub

        Private Shared Sub DumpAttyCalendarList(ByVal result As CoSoapNode)
            Dim c As CoSoapNode
            Dim i As IEnumerator

            Console.WriteLine("--------")
            Console.WriteLine("Retrieved " + result.Nodes.Count.ToString + " results")

            i = result.Nodes.GetEnumerator
            While i.MoveNext
                c = CType(i.Current, CoSoapNode)
                If c.Name = "attorneyCalendarList" Then
                    Dim descr, startTime, calDate As String
                    descr = c.Nodes.ItemByName("descr").Value.ToString
                    startTime = c.Nodes.ItemByName("startTime").Value.ToString
                    calDate = c.Nodes.ItemByName("calDate").Value.ToString
                    Console.WriteLine("{0} at {1} on {2}", _
                        descr, _
                        DateTime.Parse(startTime).ToShortTimeString, _
                        DateTime.Parse(calDate).ToShortDateString)
                End If
            End While

            Console.WriteLine("--------")
        End Sub

        Private Shared Sub RunCaseSearch1(ByVal http As HTTPTransport)
            Dim env As New CoEnvelope()
            Dim input, caseRange As CoSoapNode

            Console.WriteLine("Running sample case search 1")

            input = InitializeEnvelope(env, "searchCases")
            input.Nodes.Create("countyNo", "44")
            caseRange = input.Nodes.Create("caseRange", Nothing)
            caseRange.Nodes.Create("year", "2001")
            caseRange.Nodes.Create("caseType", "CF")
            caseRange.Nodes.Create("start", "50")
            caseRange.Nodes.Create("end", "60")

            RunStandardCaseSearch(http, env)
        End Sub

        Private Shared Sub RunCaseSearch3(ByVal http As HTTPTransport)
            Dim env As New CoEnvelope()
            Dim input, partyName As CoSoapNode

            Console.WriteLine("Running sample case search 3")

            input = InitializeEnvelope(env, "searchCases")
            partyName = input.Nodes.Create("partyName", Nothing)
            partyName.Nodes.Create("lastName", "brant")
            partyName.Nodes.Create("firstName", "peter")

            RunStandardCaseSearch(http, env)
        End Sub

        Private Shared Sub RunStandardCaseSearch(ByVal http As HTTPTransport, _
                ByVal env As CoEnvelope)
            http.Send(URL, env.Serialize)

            env.Parse(http)

            DumpCaseList(env.Parameters.Item(0))
        End Sub

        Private Shared Function InitializeEnvelope( _
                ByVal env As CoEnvelope, _
                ByVal methodName As String) As CoSoapNode

            env.EncodingStyle = ""
            env.SetMethod(methodName, WCCA2_NAMESPACE)

            InitializeEnvelope = env.Parameters.Create("input", WCCA2_NAMESPACE)
        End Function

        Private Shared Sub DumpCaseList(ByVal caseSearchResult As CoSoapNode)
            Dim c As CoSoapNode
            Dim i As IEnumerator

            Console.WriteLine("--------")
            Console.WriteLine("Retrieved " + caseSearchResult.Nodes.Count.ToString + " results")

            i = caseSearchResult.Nodes.GetEnumerator
            While i.MoveNext
                c = CType(i.Current, CoSoapNode)
                If c.Name = "caseInfoList" Then
                    Console.WriteLine("{0:-15} {1}", _
                        c.Nodes.ItemByName("caseNo").Value, _
                        c.Nodes.ItemByName("caption").Value)
                End If
            End While

            Console.WriteLine("--------")
        End Sub

        Private Shared Function GetTransport() As HTTPTransport
            Dim result As New HTTPTransport()

            result.SOAPAction = ""
            result.Authentication(USER, PASSWORD)

            GetTransport = result
        End Function

        Private Shared Function FormatName(ByVal node As CoSoapNode) As String
            Dim builder As New StringBuilder()
            Dim nameF, nameM, nameL, suffix As String

            Dim i As IEnumerator
            Dim c As CoSoapNode

            i = node.Nodes.GetEnumerator
            While i.MoveNext
                c = CType(i.Current, CoSoapNode)
                Select Case c.Name
                    Case "nameF"
                        nameF = c.Value.ToString
                    Case "nameM"
                        nameM = c.Value.ToString
                    Case "nameL"
                        nameL = c.Value.ToString
                    Case "suffix"
                        suffix = c.Value.ToString
                End Select
            End While

            If nameL Is Nothing Then
                Return ""
            Else
                builder.Append(nameL)
            End If

            If Not IsNothing(nameF) Or Not IsNothing(nameM) Or Not IsNothing(suffix) Then
                builder.Append(", ")
            End If

            If Not IsNothing(nameF) Then
                builder.Append(nameF)
                If Not IsNothing(nameM) Or Not IsNothing(suffix) Then
                    builder.Append(" ")
                End If
            End If

            If Not IsNothing(nameM) Then
                builder.Append(nameM)
                If Not IsNothing(suffix) Then
                    builder.Append(" ")
                End If
            End If

            If Not IsNothing(suffix) Then
                builder.Append(suffix)
            End If

            Return builder.ToString
        End Function
    End Class
End Namespace
